package com.example.defaultshop;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class RemoveDialog extends Dialog {
	private ProgressBar bar;
	private TextView message;
	private ImageView image;
	private Button yes, no;
	
	private BasketActivity context;
	private Item item;
	
	private int index, shopId, basketId;
	
	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			bar.setVisibility(View.VISIBLE);
			yes.setVisibility(View.GONE);
			no.setVisibility(View.GONE);
			new BasketTask().execute();
		}
	};
	
	public RemoveDialog(Context context, Bitmap bitmap, Item item, int index, int shopId, int basketId) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_remove);
		setCancelable(false);
		
		this.context = (BasketActivity) context;
		this.basketId = basketId;
		this.shopId = shopId;
		this.index = index;
		this.item = item;
		
		bar = (ProgressBar) findViewById(R.id.bar);
		
		message = (TextView) findViewById(R.id.lblRemoveItem);
		message.setText(context.getResources().getString(R.string.dialog_remove_start) + " '" + item.getAmount() + " x " + item.getName() + "' " + context.getResources().getString(R.string.dialog_remove_end));
		
		image = (ImageView) findViewById(R.id.imgRemoveImage);
		image.setImageBitmap(bitmap);
		
		yes = (Button) findViewById(R.id.btnYes);
		yes.setOnClickListener(listener);
		
		no = (Button) findViewById(R.id.btnNo);
		no.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}
	
	private class BasketTask extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Void... params) {
			try {
				InputStream in = Server.post(Server.DELETE, Server.getDeleteBasketItemXml(item.getIdx(), shopId, basketId));
				XmlPullParser parser = Server.toXmlParser(in);
				return XmlParser.getDeleteResponse(parser);
			} catch (IOException e) {
				Log.e("Shop", "IOException: " + e.getMessage());
				return false;
			} catch (NullPointerException e) {
    			Log.e("Shop", "NullPointerException: " + e.getMessage());
				return false;
			} catch (XmlPullParserException e) {
    			Log.e("Shop", "XmlPullParserException: " + e.getMessage());
				return false;
			} catch (IllegalArgumentException e) {
    			Log.e("Shop", "IllegalArgumentException: " + e.getMessage());
				return false;
			}
		}
		
		@Override
	    protected void onPostExecute(Boolean success) {
			if (success) {
				context.onItemRemoved(index);
			} else {
				Toast.makeText(context, R.string.error_item, Toast.LENGTH_LONG).show();
			}
			dismiss();
		}
	}
}